package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.entity.Member;
import com.project.entity.ProMember;
import com.project.entity.TeacherMember;
import com.project.entity.UserMember;
import com.project.mapper.ProMemberMapper;
import com.project.service.ProMemberService;
import com.project.service.UserMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

@Service
public class ProMemberServiceImpl extends ServiceImpl<ProMemberMapper, ProMember> implements ProMemberService {
    @Autowired
    private UserMemberService userMemberService;

    //1.查询成员(需要前端上传一个身份identity)
    public Member getMember(Long userId, int identity) throws Exception {
        //返回usermember类型
//        LambdaQueryWrapper<UserMember> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(UserMember::getUserId,userId);
//        UserMember serviceOne = userMemberService.getOne(queryWrapper);
        //返回member类型
        LambdaQueryWrapper<UserMember> queryWrapper=new LambdaQueryWrapper<>();
       queryWrapper.eq(UserMember::getUserId,userId);
        UserMember serviceOne = userMemberService.getOne(queryWrapper);

            if(serviceOne.getIdentity()>0){
                return new Member();
            }else {

                Member member = new Member();
                member.setUserId(serviceOne.getUserId());
                member.setPhone(serviceOne.getPhone());
                member.setName(serviceOne.getName());
                member.setMajor(serviceOne.getMajor());
                member.setDepartment(serviceOne.getDepartment());

                return member;
            }

    }
    //2.查询老师(需要前端上传一个身份identity)
    public TeacherMember getMemberTeacher(Long userId, int identity) throws Exception {
        //返回usermember类型
//        LambdaQueryWrapper<UserMember> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(UserMember::getUserId,userId);
//        UserMember serviceOne = userMemberService.getOne(queryWrapper);
        //返回member类型
        LambdaQueryWrapper<UserMember> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserMember::getUserId,userId);
        UserMember serviceOne = userMemberService.getOne(queryWrapper);


        //如果在学生框输入老师，则报错
        if(serviceOne.getIdentity()==0){
            throw new Exception("查无此人");
        }else{
            TeacherMember member=new TeacherMember();
            member.setUserId(serviceOne.getUserId());
            member.setPhone(serviceOne.getPhone());
            member.setName(serviceOne.getName());
            member.setMajor(serviceOne.getMajor());
            member.setDepartment(serviceOne.getDepartment());
            member.setPosition(serviceOne.getPosition());

            return member;
        }

    }



}
